package com.kekezu.kppw.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tsz.afinal.FinalDb;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.widget.Toast;

import com.kekezu.kppw.bean.UserBean;
import com.kekezu.kppw.utils.ConfigInc;
import com.kekezu.kppw.utils.HttpUtils;

public class MallData {
	static FinalDb db;

	/**
	 * 
	 * @param strName
	 * @param strCateId
	 * @param intNew
	 * @param intOrder
	 * @param page
	 * @param context
	 * @return
	 */
	public static ArrayList<HashMap<String, Object>> getMallShop(
			String strName, String strCateId, String intOrder, int page,
			Context context) {
		ArrayList<HashMap<String, Object>> taskList = new ArrayList<HashMap<String, Object>>();
		String urlString = ConfigInc.getServiceAdress(context) + "shopList?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		Map<String, String> params = new HashMap<String, String>();
		params.put("name", strName);
		params.put("cate_id", strCateId);
		params.put("desc", intOrder);
		params.put("page", "" + page);

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");
			String resData = jsonObject.getString("data");
			JSONObject jsondata = new JSONObject(resData);
			JSONArray jsonArray = jsondata.getJSONArray("data");

			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				for (int i = 0; i < jsonArray.length(); i++) {
					HashMap<String, Object> map = new HashMap<String, Object>();
					JSONObject itemJsonObject = jsonArray.optJSONObject(i);
					map.put("id", itemJsonObject.getString("id"));
					map.put("uid", itemJsonObject.getString("uid"));
					map.put("shop_open", itemJsonObject.getString("shop_open"));
					map.put("shop_pic", itemJsonObject.getString("shop_pic"));
					map.put("shop_name", itemJsonObject.getString("shop_name"));
					map.put("total_comment",
							itemJsonObject.getString("total_comment"));
					map.put("good_comment",
							itemJsonObject.getString("good_comment"));
					map.put("cate_name", itemJsonObject.getString("cate_name"));
					map.put("city_name", itemJsonObject.getString("city_name"));
					map.put("percent", itemJsonObject.getString("percent")
							+ "%");
					map.put("isEnterprise",
							itemJsonObject.getString("isEnterprise"));
					map.put("bank", itemJsonObject.getString("bank"));
					map.put("alipay", itemJsonObject.getString("alipay"));
					map.put("email", itemJsonObject.getString("email"));
					map.put("realname", itemJsonObject.getString("realname"));

					taskList.add(map);
				}
			} else {
				Toast.makeText(context, resMsg, Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return taskList;
	}

	public static Map<String, String> getMallShop222(String strName,
			String strCateId, int intNew, int intOrder, int page,
			Context context) {
		Map<String, String> taskList = new HashMap<String, String>();
		String urlString = ConfigInc.getServiceAdress(context) + "shopList?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		Map<String, String> params = new HashMap<String, String>();
		params.put("name", strName);
		params.put("cate_id", strCateId);
		params.put("new", "" + intNew);
		params.put("good_order", "" + intOrder);
		params.put("page", "" + page);

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			if (jsonObject.getJSONArray("data").length() <= 0) {
				taskList.put("datasize", "0");
				taskList.put("data", jsonObject.get("recommend").toString());
			} else {
				taskList.put("datasize", "1");
				taskList.put("data", jsonObject.get("data").toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return taskList;
	}

	public static ArrayList<HashMap<String, Object>> getMallData(
			String strdata, Context context) {
		ArrayList<HashMap<String, Object>> taskList = new ArrayList<HashMap<String, Object>>();
		try {
			JSONArray jsonArray = new JSONArray(strdata);
			for (int i = 0; i < jsonArray.length(); i++) {
				HashMap<String, Object> map = new HashMap<String, Object>();
				JSONObject itemJsonObject = jsonArray.optJSONObject(i);
				map.put("id", itemJsonObject.getString("id"));
				map.put("uid", itemJsonObject.getString("uid"));
				map.put("shop_pic", itemJsonObject.getString("shop_pic"));
				map.put("shop_name", itemJsonObject.getString("shop_name"));
				map.put("total_comment",
						itemJsonObject.getString("total_comment"));
				map.put("good_comment",
						itemJsonObject.getString("good_comment"));
				map.put("cate_name", itemJsonObject.getString("cate_name"));
				map.put("city_name", itemJsonObject.getString("city_name"));
				map.put("percent", "100" + "%");
				map.put("isEnterprise",
						itemJsonObject.getString("isEnterprise"));
				map.put("bank", itemJsonObject.getString("bank"));
				map.put("alipay", itemJsonObject.getString("alipay"));
				map.put("email", itemJsonObject.getString("email"));
				map.put("realname", itemJsonObject.getString("realname"));

				taskList.add(map);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return taskList;
	}

	/**
	 * 威客商城-作品，服务
	 * 
	 * @param type
	 * @param strName
	 * @param strCateId
	 * @param intNew
	 * @param intSales
	 * @param intCash
	 * @param page
	 * @param context
	 * @return
	 */
	public static ArrayList<HashMap<String, Object>> getMallWork(int type,
			String strName, String strCateId, String strDesc, int page,
			Context context) {
		ArrayList<HashMap<String, Object>> taskList = new ArrayList<HashMap<String, Object>>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "commodityList?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		Map<String, String> params = new HashMap<String, String>();
		params.put("name", strName);
		params.put("type", "" + type);
		params.put("cate_id", strCateId);
		params.put("desc", strDesc);
		params.put("page", "" + page);

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");
			String resData = jsonObject.getString("data");

			JSONObject jsondata = new JSONObject(resData);

			JSONArray jsonArray = jsondata.getJSONArray("data");

			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				if (jsonArray.length() > 0) {
					for (int i = 0; i < jsonArray.length(); i++) {
						HashMap<String, Object> map = new HashMap<String, Object>();
						JSONObject itemJsonObject = jsonArray.optJSONObject(i);

						map.put("id", itemJsonObject.getString("id"));
						map.put("uid", itemJsonObject.getString("uid"));
						map.put("shop_id", itemJsonObject.getString("shop_id"));
						map.put("cate_id", itemJsonObject.getString("cate_id"));
						map.put("title", itemJsonObject.getString("title"));
						map.put("cash", itemJsonObject.getString("cash"));
						map.put("unit", "元" + itemJsonObject.getString("unit"));
						map.put("cover", itemJsonObject.getString("cover"));
						map.put("sales_num",
								itemJsonObject.getString("sales_num"));
						map.put("city_name",
								itemJsonObject.getString("city_name"));
						map.put("cate_name",
								itemJsonObject.getString("cate_name"));
						map.put("percent", itemJsonObject.getString("percent"));
						taskList.add(map);
					}
				} else {
					Toast.makeText(context, "无数据", Toast.LENGTH_SHORT).show();
				}
			} else {
				Toast.makeText(context, resMsg, Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return taskList;
	}

	/**
	 * 收藏的店铺
	 * 
	 * @param page
	 * @param context
	 * @return
	 */
	public static ArrayList<HashMap<String, Object>> getShopColl(int page,
			Context context) {
		ArrayList<HashMap<String, Object>> taskList = new ArrayList<HashMap<String, Object>>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "shop/myCollect?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		Map<String, String> params = new HashMap<String, String>();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		params.put("token", users.get(0).getToken());
		params.put("page", "" + page);

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");
			String resData = jsonObject.getString("data");

			JSONObject jsondata = new JSONObject(resData);

			JSONArray jsonArray = jsondata.getJSONArray("data");

			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				if (jsonArray.length() > 0) {
					for (int i = 0; i < jsonArray.length(); i++) {
						HashMap<String, Object> map = new HashMap<String, Object>();
						JSONObject itemJsonObject = jsonArray.optJSONObject(i);

						map.put("id", itemJsonObject.getString("id"));
						// map.put("uid", itemJsonObject.getString("uid"));
						map.put("shop_pic",
								itemJsonObject.getString("shop_pic"));
						map.put("shop_name",
								itemJsonObject.getString("shop_name"));
						map.put("total_comment",
								itemJsonObject.getString("total_comment"));
						map.put("good_comment",
								itemJsonObject.getString("good_comment"));
						map.put("cate_name",
								itemJsonObject.getString("cate_name"));
						map.put("city_name",
								itemJsonObject.getString("city_name"));

						map.put("isEnterprise",
								itemJsonObject.getString("isEnterprise"));
						map.put("bank", itemJsonObject.getString("bank"));
						map.put("alipay", itemJsonObject.getString("alipay"));
						map.put("email", itemJsonObject.getString("email"));
						map.put("realname",
								itemJsonObject.getString("realname"));

						map.put("percent", "100%");

						taskList.add(map);
					}
				} else {
					Toast.makeText(context, "无数据", Toast.LENGTH_SHORT).show();
				}
			} else {
				Toast.makeText(context, resMsg, Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return taskList;
	}

	/**
	 * 推荐店铺
	 * 
	 * @param context
	 * @return
	 */
	public static ArrayList<HashMap<String, Object>> hotShop(Context context,
			int type) {
		ArrayList<HashMap<String, Object>> taskList = new ArrayList<HashMap<String, Object>>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/hotShop?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		Map<String, String> params = new HashMap<String, String>();
		params.put("type", "" + type);
		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");

			JSONArray jsonArray = jsonObject.getJSONArray("data");

			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				if (type == 1) {
					for (int i = 0; i < jsonArray.length(); i++) {
						HashMap<String, Object> map = new HashMap<String, Object>();
						JSONObject itemJsonObject = jsonArray.optJSONObject(i);

						map.put("id", itemJsonObject.getString("id"));
						map.put("uid", itemJsonObject.getString("uid"));
						map.put("shop_pic",
								itemJsonObject.getString("shop_pic"));
						// map.put("shop_open",
						// itemJsonObject.getString("shop_open"));
						map.put("shop_name",
								itemJsonObject.getString("shop_name"));
						map.put("total_comment",
								itemJsonObject.getString("total_comment"));
						map.put("good_comment",
								itemJsonObject.getString("good_comment"));
						map.put("cate_name", itemJsonObject.getString("tags"));
						map.put("city_name",
								itemJsonObject.getString("city_name"));
						map.put("percent", itemJsonObject.getString("percent")
								+ "%");
						map.put("isEnterprise",
								itemJsonObject.getString("isEnterprise"));

						map.put("bank", itemJsonObject.getString("bank"));
						map.put("alipay", itemJsonObject.getString("alipay"));
						map.put("email", itemJsonObject.getString("email"));
						map.put("realname",
								itemJsonObject.getString("realname"));
						taskList.add(map);
					}
				} else {
					for (int i = 0; i < jsonArray.length(); i++) {
						HashMap<String, Object> map = new HashMap<String, Object>();
						JSONObject itemJsonObject = jsonArray.optJSONObject(i);

						map.put("id", itemJsonObject.getString("id"));
						map.put("uid", itemJsonObject.getString("uid"));
						map.put("shop_id", itemJsonObject.getString("shop_id"));
						map.put("title", itemJsonObject.getString("title"));
						map.put("cover", itemJsonObject.getString("cover"));
						map.put("cash", itemJsonObject.getString("cash"));
						map.put("unit", itemJsonObject.getString("unit"));
						map.put("sales_num",
								itemJsonObject.getString("sales_num"));
						map.put("comments_num",
								itemJsonObject.getString("comments_num"));
						map.put("good_comment",
								itemJsonObject.getString("good_comment"));
						map.put("percent", itemJsonObject.getString("percent"));
						taskList.add(map);
					}
				}
			} else {
				Toast.makeText(context, resMsg, Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return taskList;
	}

}
